package gui;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTable;

public class registercourse extends JFrame {
    private JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JButton jButton1 = new JButton();
    String snames;
    String cname;
    private JComboBox jComboBox1 = new JComboBox();
    private JComboBox jComboBox2 = new JComboBox();
    private JLabel jLabel2 = new JLabel();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();


    public registercourse() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(525, 342));
        this.setVisible(true);
        jPanel1.setBounds(new Rectangle(0, 0, 525, 315));
        jPanel1.setLayout(null);
        jLabel1.setText("Students name :");
        jLabel1.setBounds(new Rectangle(35, 35, 125, 25));
        jButton1.setText("show");
        jButton1.setBounds(new Rectangle(40, 160, 75, 21));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        jButton1_actionPerformed(e);
                    } catch (SQLException f) {
                        JOptionPane.showMessageDialog(null, "error "+f.getMessage());
                    }
                }
            });
        jComboBox1.setBounds(new Rectangle(145, 35, 200, 20));
        jComboBox1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        jComboBox1_actionPerformed(e);
                    } catch (SQLException f) {
                    }
                }
            });
        jComboBox2.setBounds(new Rectangle(145, 95, 200, 20));
        jComboBox2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jComboBox2_actionPerformed(e);
                }
            });
        jLabel2.setText("courses name :");
        jLabel2.setBounds(new Rectangle(35, 90, 90, 25));
        jButton2.setText("register");
        jButton2.setBounds(new Rectangle(40, 225, 75, 20));
        jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton2_actionPerformed(e);
                }
            });
        jButton3.setText("back");
        jButton3.setBounds(new Rectangle(120, 225, 75, 21));
        jButton3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    jButton3_actionPerformed(e);
                }
            });
        jPanel1.add(jButton3, null);
        jPanel1.add(jButton2, null);
        jPanel1.add(jLabel2, null);
        jPanel1.add(jComboBox2, null);
        jPanel1.add(jComboBox1, null);
        jPanel1.add(jButton1, null);
        jPanel1.add(jLabel1, null);
        this.getContentPane().add(jPanel1, null);
        
        
        
    }
    
    private void jButton1_actionPerformed(ActionEvent e) throws SQLException {
        
        //----------------
        database t=new database();
        String select=" select * from students ";
        String select1="select * from course ";
        ResultSet rs=t.check(select);
        ResultSet re=t.check(select1);
       while(rs.next()){
                String studentname=rs.getString(2);
                jComboBox1.addItem(studentname);

            }
       while(re.next()){
           
           String coursename=re.getString(2);
           jComboBox2.addItem(coursename);

       }
            
    }

    private void jComboBox1_actionPerformed(ActionEvent e) throws SQLException {
        
        snames=jComboBox1.getSelectedItem().toString();
    }

    private void jComboBox2_actionPerformed(ActionEvent e) {
        cname=jComboBox2.getSelectedItem().toString();
    }

    private void jButton2_actionPerformed(ActionEvent e) {
        //register
        database a=new database();
        String ins=" insert into re_course  values('"+snames+"','"+cname+"')";
        if(a.insert(ins)){
            JOptionPane.showMessageDialog(null, " subject registered successfully ");
        }else{
            JOptionPane.showMessageDialog(null, "error in insert ");
        }
        
        } 
        
    

    private void jButton3_actionPerformed(ActionEvent e) {
   
        //back button
        real_project r=new real_project();
        this.setVisible(false);
    }
}
